跳到主要内容

JZ39 平衡二叉树

https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222

import java.lang.Math;

public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
return getDepth(root) != -1;
}

private int getDepth(TreeNode root) {
if (root == null) return 0;

int left = getDepth(root.left);
if (left == -1) return -1;

int right = getDepth(root.right);
if (right == -1) return -1;

// 检查高度差是否大于 1
return Math.abs(left - right) > 1 ? -1 : 1 + Math.max(left, right);
}
}